define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/highsea/index' + location.search,
                    del_url: 'business/highsea/del',
                    apply_url: 'business/highsea/apply',
                    allot_url: 'business/highsea/allot',
                    multi_url: 'business/highsea/multi',
                    import_url: 'business/highsea/import',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'gender', title: __('Gender'), searchList: {"0":__('Secrecy'),"1":__('Male'),"2":__('FeMale')}, formatter: Table.api.formatter.normal},
                        {field: 'source.name', title: __('Source'),searchList:Config.sourceList},
                        {field: 'email', title: __('Email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons:[
                            {
                                name: 'apply', 
                                icon: 'fa fa-plus', 
                                title: '领取',
                                confirm: '确定要领取吗', 
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-success btn-ajax',
                                url: $.fn.bootstrapTable.defaults.extend.apply_url,
                                success: () => $(".btn-refresh").trigger("click")
                            },
                            {
                                name: 'allot',
                                title: '分配',
                                icon: 'fa fa-share',
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-success btn-xs btn-dialog',
                                url: $.fn.bootstrapTable.defaults.extend.allot_url,
                            }
                        ]}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $('.btn-apply').click(function(){
                var ids = Table.api.selectedids(table);
                
                layer.confirm('确定要领取吗?', { title: '领取', btn: ['是', '否'] },function (index) {
                    $.post($.fn.bootstrapTable.defaults.extend.apply_url, {ids: ids}, function (response) {
                        layer.close(index)

                        if(response.code) 
                        {
                            Toastr.success(response.msg)
                            $(".btn-refresh").trigger('click');
                        }else
                        {
                            Toastr.error(response.msg)
                        }
                    }, 'json')
                })
            })
            $('.btn-allot').on('click', function () {
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.allot_url + `?ids=${ids}`, '分配')
            });
        },
        allot: function(){
            Table.api.init({
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: location.href,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search : false,
                showExport: false,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'gender_text', title: __('Gender'), formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
